var duoguanType = {
    "1":[],
    "2":[],
    "3":['3串3/3_3', '3串4/3_4'],
    "4":['4串4/4_4', '4串5/4_5', '4串6/4_6', '4串11/4_11'],
    "5":['5串5/5_5', '5串6/5_6', '5串10/5_10', '5串16/5_16', '5串20/5_20', '5串26/5_26'],
    "6":[ "6串6/6_6","6串7/6_7","6串15/6_15","6串20/6_20","6串22/6_22","6串35/6_35", "6串42/6_42","6串50/6_50","6串57/6_57"],
    "7":["7串7/7_7","7串8/7_8","7串21/7_21","7串35/7_35","7串120/7_120"],
    "8":[ "8串8/8_8","8串9/8_9","8串28/8_28","8串56/8_56","8串70/8_70","8串247/8_247"],
}
var Football = {
    gameListsData: {}
}
Football.setGameListsData = (array) => {
    let obj = {};
    for (let i = 0; i < array.length; i++) {
        obj[array[i].number] = array[i];
    }
    Football.gameListsData = obj;
}
Football.PRICE = 2;
Football.SINGLE_TYPE={
    '混合投注': 1,
    '胜平负': 2,
    '半全场': 3,
    '比分': 4,
    '进球数': 5,
}
Football.TYPE = {
    '单关': 1,
    '2串1': 2,
    '3串1': 3,
    '4串1': 4,
    '5串1': 5,
    '6串1': 6,
    '7串1': 7,
    '8串1': 8
}
Football.Danguan = {
    '1': '单关/1_1',
    '2': '2串1/2_1',
    '3': '3串1/3_1',
    '4': '4串1/4_1',
    '5': '5串1/5_1',
    '6': '6串1/6_1',
    '7': '7串1/7_1',
    '8': '8串1/8_1',
}
Football.TYPE_NAME=[
    ['胜','平','负'],
    ['让胜','让平','让负'],
    ["1:0", "2:0", "2:1", "3:0", "3:1", "3:2", "4:0", "4:1", "4:2", "5:0", "5:1", "5:2", "胜其他", "0:0", "1:1", "2:2", "3:3", "平其他", "0:1", "0:2", "0:3", "0:4", "0:5", "1:2", "1:3", "1:4", "1:5", "2:3", "2:4", "2:5", "负其他"],
    ['0','1','2','3','4','5','6','7+'],
    ['胜-胜','胜-平','胜-负','平-胜','平-平','平-负','负-胜','负-平','负-负']
];
Football.setDuoguanLists = (count) => {
    let lists = [];
    for (let k in duoguanType) {
        if (Number(k) > count) {
            break;
        }
        if (Object.keys(duoguanType[k]) == 0) {
            continue;
        }
        lists = lists.concat(duoguanType[k]);
    }
    return lists;
}
var BJBetWagerCount = new Array(16);
for (var i = 0; i < BJBetWagerCount.length; i++) {
    BJBetWagerCount[i] = new Array(16);
}
BJBetWagerCount[1][1] = "1";
BJBetWagerCount[2][1] = "1,2";
BJBetWagerCount[2][2] = "1/2";
BJBetWagerCount[3][1] = "1,2,3";
BJBetWagerCount[3][2] = "1/2,1/3,2/3";
BJBetWagerCount[3][3] = "1/2/3";
BJBetWagerCount[4][1] = "1,2,3,4";
BJBetWagerCount[4][2] = "1/2,1/3,2/3,1/4,2/4,3/4";
BJBetWagerCount[4][3] = "1/2/3,1/2/4,1/3/4,2/3/4";
BJBetWagerCount[4][4] = "1/2/3/4";
BJBetWagerCount[5][1] = "1,2,3,4,5";
BJBetWagerCount[5][2] = "1/2,1/3,2/3,1/4,2/4,3/4,1/5,2/5,3/5,4/5";
BJBetWagerCount[5][3] = "1/2/3,1/2/4,1/3/4,2/3/4,1/2/5,1/3/5,2/3/5,1/4/5,2/4/5,3/4/5";
BJBetWagerCount[5][4] = "1/2/3/4,1/2/3/5,1/2/4/5,1/3/4/5,2/3/4/5";
BJBetWagerCount[5][5] = "1/2/3/4/5";
BJBetWagerCount[6][1] = "1,2,3,4,5,6";
BJBetWagerCount[6][2] = "1/2,1/3,2/3,1/4,2/4,3/4,1/5,2/5,3/5,4/5,1/6,2/6,3/6,4/6,5/6";
BJBetWagerCount[6][3] = "1/2/3,1/2/4,1/3/4,2/3/4,1/2/5,1/3/5,2/3/5,1/4/5,2/4/5,3/4/5,1/2/6,1/3/6,2/3/6,1/4/6,2/4/6,3/4/6,1/5/6,2/5/6,3/5/6,4/5/6";
BJBetWagerCount[6][4] = "1/2/3/4,1/2/3/5,1/2/4/5,1/3/4/5,2/3/4/5,1/2/3/6,1/2/4/6,1/3/4/6,2/3/4/6,1/2/5/6,1/3/5/6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4/6/7/8,1/3/5/6/7/8,1/4/5/6/7/8,2/3/4/5/6/8,2/3/4/5/7/8,2/3/4/6/7/8,2/3/5/6/7/8,2/4/5/6/7/8,3/4/5/6/7/8";
BJBetWagerCount[8][7] = "1/2/3/4/5/6/7,1/2/3/4/5/6/8,1/2/3/4/5/7/8,1/2/3/4/6/7/8,1/2/3/5/6/7/8,1/2/4/5/6/7/8,1/3/4/5/6/7/8,2/3/4/5/6/7/8";
BJBetWagerCount[8][8] = "1/2/3/4/5/6/7/8";
BJBetWagerCount[9][9] = "1/2/3/4/5/6/7/8/9";
BJBetWagerCount[10][10] = "1/2/3/4/5/6/7/8/9/10";
BJBetWagerCount[11][11] = "1/2/3/4/5/6/7/8/9/10/11";
BJBetWagerCount[12][12] = "1/2/3/4/5/6/7/8/9/10/11/12";
BJBetWagerCount[13][13] = "1/2/3/4/5/6/7/8/9/10/11/12/13";
BJBetWagerCount[14][14] = "1/2/3/4/5/6/7/8/9/10/11/12/13/14";
BJBetWagerCount[15][15] = "1/2/3/4/5/6/7/8/9/10/11/12/13/14/15";


function arrcl(arr, n, z) {
    var r = [];
    fn([], arr, n);
    return r;
    function fn(t, a, n) {
        if (n === 0 || z && r.length == z) {
            return r[r.length] = t;
        }
        for (var i = 0, l = a.length - n; i <= l; i++) {
            if (!z || r.length < z) {
                var b = t.slice();
                b.push(a[i]);
                fn(b, a.slice(i + 1), n - 1);
            }
        }
    }
}

function arral(A2, fn) {
    var n = 0, codes = [], code = [], isTest = typeof fn == 'function', stop;
    each(A2, n);
    function each(A2, n) {
        if (stop || n >= A2.length) {
            if (isTest && false === fn(code)) {
                stop = true;
            } else {
                codes.push(code.slice());
                code.length = n - 1;
            }
        } else {
            var cur = A2[n];
            for (var i = 0, j = cur.length; i < j; i++) {
                code.push(cur[i]);
                each(A2, n + 1);
            }
            if (n) {
                code.length = n - 1;
            }
        }
    }
    return codes;
}

function zhuHeGetZuShu(ggWay, zuHe, c) {
    var zhus = 0, data = [], maxs = 0.00, mins = 0.00;
    var voteType = GetVotetype(ggWay);
    var voteArr = voteType.split(" ");
    for (var sai = 0, slen = zuHe.length; sai < slen; sai++) {
        var thisZuHe = zuHe[sai];
        for (var v = 0, vlen = voteArr.length; v < vlen; v++) {
            var vVal = voteArr[v];
            var thisBetWagerCount = BJBetWagerCount[c][vVal];
            var thisBetWagerCountArr = thisBetWagerCount.split(",");
            for (var betV = 0, blen = thisBetWagerCountArr.length; betV < blen; betV++) {
                var betZuHeVal = thisBetWagerCountArr[betV];
                var betZuHeArr = betZuHeVal.split("/");
                var zc = 1, tdata = [], max = 0;
                for (var bz = 0, bzlen = betZuHeArr.length; bz < bzlen; bz++) {
                    var ab = thisZuHe[Number(betZuHeArr[bz]) - 1];
                    var thisVal = ab.length;
                    tdata = cartesian(tdata, ab);
                    zc *= Number(thisVal);
                }
                if (data.length === 0) {
                    data = tdata;
                } else
                    data.push.apply(data, tdata);
                zhus += zc;
            }
        }
    }
    return { zhus: zhus, data: data };
}
function cartesian(a, b) {
    var ret = [];
    if (a.length === 0) {
        b.forEach(function (c) {
            ret.push([c]);

        });
    } else {
        a.forEach(function (a) {
            b.forEach(function (b) {
                var e = a.concat();
                e.push(b);
                ret.push(e);
            });
        });
    }
    return ret;
}
function cartesianProductOf(a, b) {
    var ret = [];
    a.forEach(function (c) {
        b.forEach(function (d) {
            //����cҪ������
            if (c instanceof Array) {
                var e = c.concat();
                e.push(d);
                ret.push(e);
            } else
                ret.push([c].concat([d]));
        });
    });
    return ret;
}

function GetVotetype(typevote) {
    switch (typevote) {
        case "1_1":
            return "1";
        case "2_1":
            return "2";
        case "2_3":
            return "1 2";
        case "3_1":
            return "3";
        case "3_3":
            return "2";
        case "3_4":
            return "2 3";
        case "4_1":
            return "4";
        case "4_2":
            return "2";
        case "4_4":
            return "3";
        case "4_5":
            return "3 4";
        case "4_6":
            return "2";
        case "4_11":
            return "2 3 4";
        case "5_1":
            return "5";
        case "5_2":
            return "2";
        case "5_5":
            return "4";
        case "5_6":
            return "4 5";
        case "5_10":
            return "2";
        case "5_16":
            return "3 4 5";
        case "5_20":
            return "2 3";
        case "5_26":
            return "2 3 4 5";
        case "6_1":
            return "6";
        case "6_2":
            return "2";
        case "6_6":
            return "5";
        case "6_7":
            return "5 6";
        case "6_15":
            return "2";
        case "6_20":
            return "3";
        case "6_22":
            return "4 5 6";
        case "6_35":
            return "2 3";
        case "6_42":
            return "3 4 5 6";
        case "6_50":
            return "2 3 4";
        case "6_57":
            return "2 3 4 5 6";
        case "7_1":
            return "7";
        case "7_7":
            return "6";
        case "7_8":
            return "6 7";
        case "7_21":
            return "5";
        case "7_35":
            return "4";
        case "7_120":
            return "2 3 4 5 6 7";
        case "8_1":
            return "8";
        case "8_8":
            return "7";
        case "8_9":
            return "7 8";
        case "8_28":
            return "6";
        case "8_56":
            return "5";
        case "8_70":
            return "4";
        case "8_247":
            return "2 3 4 5 6 7 8";
        default:
            if (typevote.indexOf("二关") > -1)
                return "2";
            if (typevote.indexOf("三关") > -1)
                return "3";
            else
                return "0";
    }
}
function myCalc(ggWay, sa) {
    var ggArr = ggWay.split("_");
    var c = Number(ggArr[0]);
    var macthNum = sa.length;
    var zuShu = 0;
    var data = { zhus: 0, data: [] };
    var saZh = arrcl(sa, c);
    for (var i = 0; i < saZh.length; i++) {
        var thispl = arral(saZh[i]);
        var d = zhuHeGetZuShu(ggWay, thispl, c);
        data.zhus += d.zhus;
        if (data.data.length === 0) {
            data.data = d.data;
        } else
            data.data.push.apply(data.data, d.data);
    }
    return data;
}
Football.betNumberToArray = (betNumber) => {
    let lists = {};
    for (let key in betNumber) {
        let keys = key.split("_");
        let { NoID, Number, TypeId, BetNo, str, odds, dan,IsDan } = betNumber[key];
        let number_type = Number + "_" + TypeId;
        if (lists.hasOwnProperty(number_type)) {
            lists[number_type].push(betNumber[key]);
        } else {
            lists[number_type] = [betNumber[key]];
        }
    }
    let newLists = {};
    for (let key in lists) {
        let keys = key.split("_");
        if (newLists.hasOwnProperty(keys[0])) {
            newLists[keys[0]].push(lists[key]);
        } else {
            newLists[keys[0]] = [lists[key]];
        }
    }
    let betArray = [];
    for (let key in newLists) {
        betArray.push(newLists[key]);
    }
    return {lists:betArray};
}
function getBonus(item,multNum){
    let bonus = 1;
    for(let i=0;i<item.length;i++){
        bonus = bonus * item[i].odds;
    }
    return (bonus * Number(multNum) * Football.PRICE).toFixed(2);
}
function getBuyNumber(item){
    let array=[];
    let Numbers=[];
    for(let i=0;i<item.length;i++){
        Numbers.push(item[i].Number);
        array.push(item[i].NoID.replace(/_/g,''));
    }
    return {
        buyNumber:array.join('|'),
        Numbers:Numbers
    };
}
function getBuyNumberB(item){
    let array=[];
    for(let i=0;i<item.length;i++){
        array.push(item[i].NoID);
    }
    return array.join('|');
}
function getBuyNumberC(betNumber,item){
    let array=[];
    for(let i=0;i<item.length;i++){
        array.push(item[i].NoID);
    }
    array.sort(function(item1,item2){
        return Number(betNumber[item1].odds) -  Number(betNumber[item2].odds);
    });
    return array.join('|');
}



function isContain(a,b){
    let count =0;
    for(let i=0;i<b.length;i++){
        if(a.includes(b[i])){
            count ++;
        }
    }
    return count;
}
Football.betNumberComb = (name, betNumber,multNum,danlists) => {    
    let {lists} = Football.betNumberToArray(betNumber);
    let betRecords = [];
    name.sort()
    for (let i = 0; i < name.length; i++) {        
        let temp = name[i].split("/");
        let data = myCalc(temp[1], lists).data;
        let tempBet =[];
        for(let j =0;j<data.length;j++){
            let {buyNumber,Numbers} = getBuyNumber(data[j]);
            if(isContain(Numbers,danlists)!=danlists.length){
                continue;
            }        
            tempBet.push({
                "name": temp[0], //过关的名称
                "buyNumber":buyNumber, // betNumber中的NoID 用|分割
                "multNum":parseInt(multNum), //倍数
                "numNo": data[j].length,  //排列组合的长度
                "bonus":getBonus(data[j],multNum),
                "odds":getBonus(data[j],1),
            });
        }
        betRecords=betRecords.concat(tempBet);
    }
    return betRecords;
}
Football.betNumberCombB = (name, betNumbers,multNum) => {
    let {lists} = Football.betNumberToArray(betNumbers);
    let betRecords = [];
    name.sort()
    for (let i = 0; i < name.length; i++) {        
        let temp = name[i].split("/");     
        let data = myCalc(temp[1], lists).data;
        let tempBet =[];
        for(let j =0;j<data.length;j++){
            let buyNumberC = getBuyNumberC(betNumbers,data[j]);
            let {buyNumber} = getBuyNumber(data[j]);            
            tempBet.push({
                "name": temp[0], //过关的名称
                "buyNumber":buyNumber, // betNumber中的NoID 用|分割
                "buyNumberC":buyNumberC, 
                "multNum":1, //倍数
                "numNo": data[j].length,  //排列组合的长度
                "bonus":getBonus(data[j],multNum),
                "odds":getBonus(data[j],1),
            });
        }

        betRecords=betRecords.concat(tempBet);
    }
    betRecords.sort(function(item1,item2){
        return Number(item1.bonus) - Number(item2.bonus);
    });
    console.log(betRecords);
    return betRecords;
}
module.exports = Football;